package com.zheng.pay.admin.manage.controller;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.zheng.common.base.BaseController;
import com.zheng.common.util.*;
import com.zheng.common.validator.LengthValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.zheng.pay.dao.model.*;
import com.zheng.pay.rpc.api.*;
import com.zheng.pay.common.constant.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;

@Controller
@Api(value = "商户通知管理", description = "商户通知管理")
@RequestMapping("/manage/mchnotify")
public class MchNotifyController  extends BaseController {

    private static final Logger _log = LoggerFactory.getLogger(MchNotifyController.class);

    @Autowired
    private PayMchNotifyService payMchNotifyService;

    @ApiOperation(value = "商户通知首页")
    @RequiresPermissions("pay:mchnotify:read")
    @RequestMapping(value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
    public String index() {
        return "/manage/mchnotify/index.jsp";
    }

    @ApiOperation(value = "商户通知列表")
    @RequiresPermissions("pay:mchnotify:read")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order,
            @RequestParam(required = false, defaultValue = "", value = "orderid") String orderid,
            @RequestParam(required = false, defaultValue = "", value = "mchId") String mchId,
            @RequestParam(required = false, defaultValue = "", value = "begintime") String begintime,
            @RequestParam(required = false, defaultValue = "", value = "endtime") String endtime,
            @RequestParam(required = false, defaultValue = "", value = "mchOrderNo") String mchOrderNo) {

        PayMchNotifyExample payMchNotifyExample= new PayMchNotifyExample();
        PayMchNotifyExample.Criteria criteria= payMchNotifyExample.createCriteria();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            payMchNotifyExample.setOrderByClause(sort + " " + order);
        }
        if (StringUtils.isNotBlank(orderid)) {
            criteria.andOrderidEqualTo(orderid);
        }
        if (StringUtils.isNotBlank(mchId)) {
            criteria.andMchidEqualTo(mchId);
        }
        if (StringUtils.isNotBlank(mchOrderNo)) {
            criteria.andMchordernoEqualTo(mchOrderNo);
        }
        if (StringUtils.isNotBlank(begintime) && StringUtils.isNotBlank(endtime)) {
            criteria.andCreatetimeBetween(DateUtil.parseDate(begintime,DateUtil.FORMAT_YYYY_MM_DD),DateUtil.parseDate(endtime,DateUtil.FORMAT_YYYY_MM_DD));
        }

        List<PayMchNotify> rows = payMchNotifyService.selectByExampleForOffsetPage(payMchNotifyExample, offset, limit);
        long total = payMchNotifyService.countByExample(payMchNotifyExample);
        Map<String, Object> result = new HashMap<>(2);
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增")
    @RequiresPermissions("pay:mchnotify:create")
    @RequestMapping(value = "/create", method = {RequestMethod.GET})
    public String create() {
        return "/manage/mchnotify/create.jsp";
    }

    @ApiOperation(value = "新增商户通知")
    @RequiresPermissions("pay:mchnotify:create")
    @RequestMapping(value = "/create", method = {RequestMethod.POST})
    @ResponseBody
    public Object create(PayMchNotify payMchNotify) {
        ComplexResult result = FluentValidator.checkAll()
                .on(payMchNotify.getOrderid(), new LengthValidator(1, 24, "订单ID"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new PayResult(PayResultConstant.INVALID_LENGTH, result.getErrors());
        }
        Date time = DateUtil.getNowDate();
        payMchNotify.setUpdatetime(time);
        payMchNotify.setCreatetime(time);
        int count = payMchNotifyService.insertSelective(payMchNotify);
        return new PayResult(PayResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "删除商户通知")
    @RequiresPermissions("pay:mchnotify:delete")
    @RequestMapping(value = "/delete/{ids}",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = payMchNotifyService.deleteByPrimaryKeys(ids);
        return new PayResult(PayResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "修改商户通知")
    @RequiresPermissions("pay:mchnotify:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.GET})
    public String update(@PathVariable("id") String id, ModelMap modelMap) {

        PayMchNotifyExample payMchNotifyExample= new PayMchNotifyExample();
        if (!StringUtils.isBlank(id) && !StringUtils.isBlank(id)) {
            payMchNotifyExample.createCriteria().andOrderidEqualTo(id);
        }
        PayMchNotify payMchNotify = payMchNotifyService.selectFirstByExample(payMchNotifyExample);
        modelMap.put("payMchNotify", payMchNotify);
        return "/manage/mchnotify/update.jsp";
    }

    @ApiOperation(value = "修改商户通知")
    @RequiresPermissions("pay:mchnotify:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.POST})
    @ResponseBody
    public Object update(@PathVariable("id") String id, PayMchNotify payMchNotify) {
        ComplexResult result = FluentValidator.checkAll()
                .on(payMchNotify.getOrderid(), new LengthValidator(1, 24, "订单ID"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new PayResult(PayResultConstant.INVALID_LENGTH, result.getErrors());
        }
        payMchNotify.setMchid(id);
        int count = payMchNotifyService.updateByPrimaryKeySelective(payMchNotify);
        return new PayResult(PayResultConstant.SUCCESS, count);
    }


}